// Copyright 2010 Google Inc. All Rights Reserved.

package br.org.imazon.sad;

import com.google.gdata.util.ServiceException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.logging.Logger;

import com.google.appengine.api.urlfetch.*;

import static com.google.appengine.api.urlfetch.FetchOptions.Builder.*;


/**
 * @author thau@google.com (Dave Thau)
 *
 */
public class EEAccess {

  //The example requires the Google GData Client library, 
  //which in turn requires the
  //Google Collection library. These can be downloaded from
  //http://code.google.com/p/gdata-java-client/downloads/list and
  //http://code.google.com/p/google-collections/downloads/list.


  private static final String SERVICE_URL =
    "http://earthengine.googleapis.com/";

  private static final Logger log = Logger.getLogger(EEAccess.class.getName());

  private EEAccess() { }

  public static String getSearch(String authKey, String query)
  throws IOException, ServiceException {

    String result = apiRequest(authKey, "GET", "search", query);
    return result;
  }



  public static String getThumbId(String authKey, String query)
  throws IOException, ServiceException {
    String result = apiRequest(authKey, "GET", "thumb", query);
    return result;
  }

  public static String getInfo(String authKey, String query) throws IOException, ServiceException {
    String result = apiRequest(authKey, "GET", "info", query);

    return result;
  }

  public static String getList(String authKey, String query) throws IOException, ServiceException {
    String result = apiRequest(authKey, "GET", "list", query);

    return result;
  }

  public static String getMapId(String authKey, String query) throws IOException, ServiceException {
    String result = apiRequest(authKey, "POST", "mapid", query);

    return result;
  }

  public static String getValue(String authKey, String query) throws IOException, ServiceException {
    String result = apiRequest(authKey, "POST", "value", query);

    return result;
  }


  private static String apiRequest(String authKey, String method, String type, String args) throws IOException, ServiceException {
    log.info("connecting to: " + SERVICE_URL + "api/" + type + "?" + args);

    HTTPMethod thisMethod = HTTPMethod.POST;

    String connectionString =SERVICE_URL + "api/" + type;
    if (method.equals("GET")) {
      connectionString += "?" + args;
      thisMethod = HTTPMethod.GET;
    }

    URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
    HTTPHeader authHeader = new HTTPHeader("Authorization", "GoogleLogin auth=" + authKey);
    HTTPRequest req = new HTTPRequest(new URL(connectionString), thisMethod,  FetchOptions.Builder.withDefaults().setDeadline(30.0));
    req.setHeader(authHeader);

    log.info("auth key: " + authKey);

    if (method.equals("POST")) {
      log.info("posting type: " + type + " args: " + args);
      req.setPayload(args.getBytes());
    }
    HTTPResponse resp = urlFetchService.fetch(req);

    return new String(resp.getContent());
  }
}

